# -*- coding: utf-8 -*-
import codecs
import json
from spiders.checkpipe import check_spider_pipeline


class JsonWithEncodingPipeline(object):

    def __init__(self):
        self.file = codecs.open('info.json', 'w', encoding='utf-8')  # 保存为json文件

    @check_spider_pipeline
    def process_item(self, item, spider):
        line = json.dumps(dict(item)) + "\n"  # 转为json的
        self.file.write(line)  # 写入文件中
        return item

    def spider_closed(self, spider):  # 爬虫结束时关闭文件
        self.file.close()
